package com.fqcar.fqdf.ui.main.marker;

import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.fqcar.fqdf.core.cache.HomeSelectKey;
import com.fqcar.fqdf.entity.CarInfo;
import com.fqcar.fqdf.entity.StubInfo;
import com.fqcar.fqdf.ui.MapWithCacheActivity;
import com.fqcar.fqdf.ui.main.holder.MainHolderHelper;
import com.fqcar.fqdf.util.Constants;
import com.fqdf.framework.android.util.CheckLogicUtil;

import java.util.ArrayList;
import java.util.List;

public class MainBaiduMarkerHelper extends BaiduMarkerHelper implements MainOverlayInterface<Overlay> {

    private MapWithCacheActivity mActivity;
    private BaiduMap mBaiduMap;

    private MainHolderHelper holderHelper;

    public MainBaiduMarkerHelper(MapWithCacheActivity activity, BaiduMap baiduMap, MainHolderHelper holder) {
        this.mActivity = activity;
        mBaiduMap = baiduMap;
        holderHelper = holder;
    }


    @Override
    public List<Overlay> createCarMarker(List<CarInfo> carInfoList) {
        if (CheckLogicUtil.isEmpty(carInfoList)) {
            return null;
        }

        List<OverlayOptions> markerOptionsList = new ArrayList<>();

        for (CarInfo item : carInfoList) {

            HomeSelectKey.Builder keyBuilder = new HomeSelectKey.Builder();
            keyBuilder.withIsForRent(item.getState() == Constants.CarState.FOR_RENT);
            keyBuilder.withIsForRentPer(item.getState() == Constants.CarState.FOR_RENT_PERCENT);
            keyBuilder.withIsCharging(item.getChargeStatus() == Constants.CarState.IS_CHARGING);
            keyBuilder.withIsOffline(item.getState() == Constants.CarState.OFFLINE);
            keyBuilder.withIsRepair(item.getState() == Constants.CarState.REPAIR);
            keyBuilder.withIsLowPowerOffline(item.getState() == Constants.CarState.LOW_POWER_OFFLINE);
            keyBuilder.withIsLowPowerPercentOffline(item.getState() == Constants.CarState.LOW_POWER_PERCENT_OFFLINE);
            keyBuilder.withIsSelect(item.isSelect());

            HomeSelectKey key = keyBuilder.build();

            Bundle bundle = new Bundle();
            bundle.putParcelable(Constants.BUNDLE_CARENTITY, item);
            bundle.putBoolean(Constants.BUNDLE_DATA, item.isSelect());

            OverlayOptions markerOptions = createOverlay(mActivity.getBitmapDescriptorFromCache(key
                    , holderHelper.getCarHolderByKey(key)), item.getLat(), item.getLng(), bundle);

            markerOptionsList.add(markerOptions);
        }

        return mBaiduMap.addOverlays(markerOptionsList);
    }

    @Override
    public Overlay createCarMarker(CarInfo item) {
        HomeSelectKey.Builder keyBuilder = new HomeSelectKey.Builder();
        keyBuilder.withIsForRent(item.getState() == Constants.CarState.FOR_RENT);
        keyBuilder.withIsForRentPer(item.getState() == Constants.CarState.FOR_RENT_PERCENT);
        keyBuilder.withIsCharging(item.getChargeStatus() == Constants.CarState.IS_CHARGING);
        keyBuilder.withIsOffline(item.getState() == Constants.CarState.OFFLINE);
        keyBuilder.withIsRepair(item.getState() == Constants.CarState.REPAIR);
        keyBuilder.withIsLowPowerOffline(item.getState() == Constants.CarState.LOW_POWER_OFFLINE);
        keyBuilder.withIsLowPowerPercentOffline(item.getState() == Constants.CarState.LOW_POWER_PERCENT_OFFLINE);
        keyBuilder.withIsSelect(item.isSelect());

        HomeSelectKey key = keyBuilder.build();

        Bundle bundle = new Bundle();
        bundle.putParcelable(Constants.BUNDLE_CARENTITY, item);
        bundle.putBoolean(Constants.BUNDLE_DATA, item.isSelect());

        OverlayOptions markerOptions = createOverlay(mActivity.getBitmapDescriptorFromCache(key
                , holderHelper.getCarHolderByKey(key)), item.getLat(), item.getLng(), bundle);

        //在地图上添加Marker，并显示
        Marker marker = (Marker) mBaiduMap.addOverlay(markerOptions);
        return marker;
    }


    @Override
    public List<Overlay> createStubMarker(List<StubInfo> stubInfoList) {
        if (CheckLogicUtil.isEmpty(stubInfoList)) {
            return null;
        }

        List<OverlayOptions> markerOptionsList = new ArrayList<>();

        for (StubInfo item : stubInfoList) {

            HomeSelectKey.Builder keyBuilder = new HomeSelectKey.Builder();
            keyBuilder.withIsStubForUse(item.getState() == Constants.StubState.NO_STUB_CHARGING);
            keyBuilder.withIsStubForUseCharing(item.getState() == Constants.StubState.IS_STUB_CHARGING);

            keyBuilder.withIsSelect(item.isSelect());

            HomeSelectKey key = keyBuilder.build();

            Bundle bundle = new Bundle();
            bundle.putParcelable(Constants.BUNDLE_STUB_ENTITY, item);
            bundle.putBoolean(Constants.BUNDLE_DATA, item.isSelect());

            OverlayOptions markerOptions = createOverlay(mActivity.getBitmapDescriptorFromCache(key
                    , holderHelper.getCarHolderByKey(key)), item.getLat(), item.getLng(), bundle);

            markerOptionsList.add(markerOptions);
        }

        return mBaiduMap.addOverlays(markerOptionsList);
    }
    @Override
    public Overlay createStubMarker(StubInfo item) {
        HomeSelectKey.Builder keyBuilder = new HomeSelectKey.Builder();
        keyBuilder.withIsStubForUse(item.getState() == Constants.StubState.NO_STUB_CHARGING);
        keyBuilder.withIsStubForUseCharing(item.getState() == Constants.StubState.IS_STUB_CHARGING);

        keyBuilder.withIsSelect(item.isSelect());

        HomeSelectKey key = keyBuilder.build();

        Bundle bundle = new Bundle();
        bundle.putParcelable(Constants.BUNDLE_STUB_ENTITY, item);
        bundle.putBoolean(Constants.BUNDLE_DATA, item.isSelect());

        OverlayOptions markerOptions = createOverlay(mActivity.getBitmapDescriptorFromCache(key
                , holderHelper.getCarHolderByKey(key)), item.getLat(), item.getLng(), bundle);
        //在地图上添加Marker，并显示
        Marker marker = (Marker) mBaiduMap.addOverlay(markerOptions);
        return marker;
    }
}
